$(function() {
	var rootlevelInnerHtml = "";
	for(let i = 0; i < 3; i++) {
		rootlevelInnerHtml += "<li role=\"presentation\">\n" +
			"<a role=\"menuitem\" tabindex=\"-1\" href=\"javascript:void(0);\" onclick=\"chooseRoot(" + i + ");\">" + i + "</a>\n" +
			"</li>";
	}
	$("#rootlevelUl").html(rootlevelInnerHtml);

	var sortLevelInnerHtml = "";
	for(let i = 0; i < 5; i++) {
		sortLevelInnerHtml += "<li role=\"presentation\">\n" +
			"<a role=\"menuitem\" tabindex=\"-1\" href=\"javascript:void(0);\" onclick=\"chooseSortLevel(" + i + ");\">" + i + "</a>\n" +
			"</li>";
	}
	$("#sortLevelUl").html(sortLevelInnerHtml);

	$("#submit").click(function() {
		var remark = $("#remark").val();
		var pathName = $("#pathName").val();
		var rootlevel = $("#rootlevel").text();
		var fatherLeaf = $("#fatherLeaf").attr("name");
		var sonLeaf = $('.sonLeaf:radio:checked').val();
		var sortLevel = $("#sortLevel").text();
		console.log("0::" + remark + "::1::" + pathName + "::2::" + rootlevel + "::3::" + fatherLeaf + "::4::" + sonLeaf + "::5::" + sortLevel);
		$.post("http://localhost:8081/study/path/addPath", {
			remark: remark,
			pathName: pathName,
			rootLeaf: rootlevel,
			fatherLeaf: fatherLeaf,
			sonLeaf: sonLeaf,
			sortLevel: sortLevel
		}, function(data) {
			console.log(data)
		})
	})
})

function chooseSortLevel(a) {
	console.log("chooseSortLevel" + a);
	$("#sortLevel").text(a);
	$("#sortLevel").attr("name", a);
}

function chooseRoot(a) {
	console.log("chooseRoot" + a);
	$("#rootlevel").text(a);
	//同事将父节点置于对应值
	$("#fatherLeaf").text(0);
	$("#fatherLeaf").attr("name", "0");
	if(a == 0) {
		return;
	}
	var fatherLeaf = a - 1;

	$.post("http://localhost:8081/study/path/findFatherLeaf", {
		fatherLeaf: fatherLeaf
	}, function(data) {
		if(data.success) {
			console.log(data.message);
			console.log(data);
			var fatherLeafList = data.list;
			var fatherLeafInnerHtml = "";
			//循环遍历返回回来的json数据
			$.each(fatherLeafList, function(key, data) {
				console.log(data.id + "::" + data.remark);
				fatherLeafInnerHtml += "<li role=\"presentation\">\n" +
					"<a role=\"menuitem\" tabindex=\"-1\" href=\"javascript:void(0);\" onclick=\"chooseFatherLeaf(" + data.id + "," + "'" + data.remark + "');\">" + data.remark + "</a>\n" +
					"</li>";
			});
			$("#fatherLeafUl").html(fatherLeafInnerHtml);
		} else {
			console.log(data.message)
		}
	})
}

function chooseFatherLeaf(a, b) {
	console.log(a, b);
	$("#fatherLeaf").text(b);
	$("#fatherLeaf").attr("name", a);
}